﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InterValometer
{
    class InterValometerTimer
    {
        public InterValometerTimer()
            : this(1)
        {
            this.Enabled = false;
            this.Started = System.DateTime.Now;
            this.Stopped = this.Started;
            this.Update();
            this.Expired = false;
            this.Elapsed = 0;
        }

        public InterValometerTimer(int duration)
        {
            this.Duration = duration;
        }

        public void Update()
        {
            if (this.Enabled)
            {
                this.Elapsed = (System.DateTime.Now - this.Started).TotalMilliseconds;

                // +100 fudge factor
                if (this.Elapsed > this.Duration * 1000 + 100)
                {
                    //this.Enabled = false;
                    this.Expired = true;
                    this.Stopped = System.DateTime.Now;
                    this.Elapsed = (this.Stopped - this.Started).TotalMilliseconds;
                }
            }
        }

        public void Start()
        {
            this.Enabled = true;
            this.Started = System.DateTime.Now;
        }

        public void Reset()
        {
            this.Enabled = false;
            this.Started = System.DateTime.Now;
            this.Stopped = this.Started;
            this.Update();
            this.Expired = false;
            this.Elapsed = 0;
        }

        public bool Enabled { get; set; }
        public int Duration { get; set; }
        public System.DateTime Started { get; set; }
        public System.DateTime Stopped { get; set; }
        public double Elapsed { get; set; }
        public bool Expired { get; set; }
    }
}
